﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.IO;

using Newtonsoft.Json;
using SH.STD.Model;
using Cmn;
using SH.STD.DataProvider;
using System.Web.SessionState;

namespace SH.STD.Web.Handler
{
    /// <summary>
    /// Summary description for ProjectEmployee
    /// </summary>
    public class Member : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.Cache.SetNoStore();
            context.Response.ContentType = "application/json;charset=gb2312";

            switch (context.Request.Params["action"])
            {
                case "loadData":
                    context.Response.Write(ConvertToJsonString());
                    break;
                case "removeData":
                    context.Response.Write(RemoveData(context.Request.Params["jsonDataString"]));
                    break;
                case "addData":
                    context.Response.Write(AddData(context.Request.Params["jsonDataString"]));
                    break;
            }
        }

        private string ConvertToJsonString()
        {
            var ID = HttpContext.Current.Request.Params["ID"].ToString().ToInt32();
            var tableID = HttpContext.Current.Request.Params["TableID"].ToString().ToInt32();
            var sql = @"
SELECT a.ID as ID,a.MemberType,b.ID as EmpID,b.Name as EmpName,b.Gender,c.ID as DepID,c.Name as DepName
FROM Member a
join Employee b
on a.EmployeeID=b.ID
join Department c
on b.DepID=c.ID
WHERE a.TableID=@TableID and a.ID=@ID and a.status=1";
            var cmdParams = new SqlParameter[]
                {
                    new SqlParameter("TableID",tableID),
                    new SqlParameter("ID",ID),
                };
            var dt = Cmn.DB.getDataTable(sql, cmdParams);
            return DataAccess.ConvertToJSON(dt, "Members");
        }

        private string RemoveData(string jsonDataString)
        {
            JavaScriptObject data = (JavaScriptObject)JavaScriptConvert.DeserializeObject(jsonDataString);
            var tableID = RepsonseInfo.Decode(data["TableID"].ToString()).ToInt32();
            var ID = RepsonseInfo.Decode(data["ID"].ToString()).ToInt32();
            var employeeID = RepsonseInfo.Decode(data["EmployeeID"].ToString()).ToInt32();

            var delSql = "delete from Member where TableID=@TableID and ID=@ID and EmployeeID=@EmployeeID";
            var cmdParams = new SqlParameter[] 
                {
                    new SqlParameter("TableID",tableID),
                    new SqlParameter("ID",ID),
                    new SqlParameter("EmployeeID",employeeID)
                };
            Cmn.DB.execSql(delSql, cmdParams);

            return JavaScriptConvert.SerializeObject(new RepsonseInfo { MSG = ID.ToString() });
        }

        private string AddData(string jsonDataString)
        {
            JavaScriptObject data = (JavaScriptObject)JavaScriptConvert.DeserializeObject(jsonDataString);
            var tableID = RepsonseInfo.Decode(data["TableID"].ToString()).ToInt32();
            var ID = RepsonseInfo.Decode(data["ID"].ToString()).ToInt32();
            var employeeID = RepsonseInfo.Decode(data["EmployeeID"].ToString()).ToInt32();
            var memberType = RepsonseInfo.Decode(data["MemberType"].ToString()).ToInt32();
            var lastModifiedBy = Cmn.Session.GetUserID();

            var delSql = "delete from Member where TableID=@TableID and ID=@ID and EmployeeID=@EmployeeID";
            var cmdParams = new SqlParameter[] 
                {
                    new SqlParameter("TableID",tableID),
                    new SqlParameter("ID",ID),
                    new SqlParameter("EmployeeID",employeeID)
                };
            Cmn.DB.execSql(delSql, cmdParams);
            var insertSql = "insert into Member (TableID,ID,EmployeeID,MemberType,LastModifiedBy) values (@TableID,@ID,@EmployeeID,@MemberType,@LastModifiedBy)";
            cmdParams = new SqlParameter[] 
                {
                    new SqlParameter("TableID",tableID),
                    new SqlParameter("ID",ID),
                    new SqlParameter("EmployeeID",employeeID),
                    new SqlParameter("MemberType",memberType),
                    new SqlParameter("LastModifiedBy",lastModifiedBy),
                };
            Cmn.DB.execSql(insertSql, cmdParams);

            return JavaScriptConvert.SerializeObject(new RepsonseInfo { MSG = ID.ToString() });
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}